package com.nebula.controller.salary;

import com.alibaba.fastjson.JSON;
import com.nebula.common.Const;
import com.nebula.common.DataGridFooter;
import com.nebula.common.PageResult;
import com.nebula.controller.BaseController;
import com.nebula.entity.Salary;
import com.nebula.common.Result;
import com.nebula.service.ISalaryService;
import com.nebula.service.impl.SalaryServiceImpl;
import com.nebula.util.DateTimeUtil;
import com.nebula.vo.SalaryVo;
import com.nebula.vo.UserVo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

import static com.nebula.controller.salary.HistorySalaryController.getResult;

/**
 * @author CcccX
 * @since 2020-08-08 10:20:01
 */
@WebServlet(name = "MySalaryController", urlPatterns = "/mySalary")
public class MySalaryController extends BaseController {

    private final ISalaryService salaryService = new SalaryServiceImpl();

    public String index(HttpServletRequest request, HttpServletResponse response) {
        String navId = request.getParameter("id");
        UserVo user = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        ISalaryService iSalaryService = new SalaryServiceImpl();
        if (iSalaryService.checkNavRole(user.getRoleId(), navId)) {
            return Const.FORWARD + Const.DEFAULT_URL + "salary/my-salary.jsp";
        }
        return Const.FORWARD + "role-error.jsp";
    }

    public String findCurrYearByUserId(HttpServletRequest request, HttpServletResponse response) {
        UserVo user = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        PageResult<SalaryVo> pageResult = salaryService.findByUserIdAndPage(user.getId(), 1, 12, false);

        Map<String, Object> map = new HashMap<>();
        map.put("total", pageResult.getTotalCount());
        map.put("rows", pageResult.getList());
        DataGridFooter dataGridFooter = salaryService.findTotalSalary(user.getId(), false);
        return getResult(pageResult, map, dataGridFooter);
    }

    public String save(HttpServletRequest request, HttpServletResponse response) {
        String issueDate = request.getParameter("issueDate");
        String salary = request.getParameter("salary");
        if (StringUtils.isBlank(issueDate) || StringUtils.isBlank(salary)) {
            return null;
        }

        String remark = request.getParameter("remark");
        String[] split = issueDate.split("-");
        if (StringUtils.isBlank(remark)) {
            remark = split[0] + "年" + Integer.parseInt(split[1]) + "月份工资";
        }
        UserVo user = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        Salary salaryCls = new Salary();
        salaryCls.setIssueDate(DateTimeUtil.strToDate(issueDate, "yyyy-MM-dd"));
        salaryCls.setRemark(remark);
        salaryCls.setSalary(new BigDecimal(salary));
        salaryCls.setIssueYear(Integer.parseInt(split[0]));
        salaryCls.setIssueMonth(Integer.parseInt(split[1]));
        salaryCls.setUserId(user.getId());
        Date date = new Date();
        salaryCls.setCreatedTime(date);
        salaryCls.setUpdatedTime(date);

        try {
            salaryService.save(salaryCls);
            return Const.JSON + JSON.toJSONString(Result.createBySuccessMessage("添加成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("添加失败"));
        }
    }

    public String findById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        SalaryVo salaryVo = salaryService.findByID(id);
        return Const.JSON + JSON.toJSONString(salaryVo);
    }

    public String update(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        String issueDate = request.getParameter("issueDate");
        String salary = request.getParameter("salary");
        if (StringUtils.isBlank(issueDate) || StringUtils.isBlank(salary)) {
            return null;
        }
        String remark = request.getParameter("remark");
        String[] split = issueDate.split("-");
        Salary salaryCls = new Salary();
        salaryCls.setId(id);
        salaryCls.setSalary(new BigDecimal(salary));
        salaryCls.setIssueYear(Integer.parseInt(split[0]));
        salaryCls.setIssueMonth(Integer.parseInt(split[1]));
        salaryCls.setRemark(remark);
        salaryCls.setIssueDate(DateTimeUtil.strToDate(issueDate, "yyyy-MM-dd"));
        Date date = new Date();
        salaryCls.setUpdatedTime(date);
        try {
            salaryService.update(salaryCls);
            return Const.JSON + JSON.toJSONString(Result.createBySuccessMessage("保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("保存失败"));
        }
    }

    public String delete(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        try {
            salaryService.delete(id);
            return Const.JSON + JSON.toJSONString(Result.createBySuccessMessage("删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("删除失败"));
        }
    }
}
